require "logUtil"
require "util"

GuanQia = {}

function GuanQia.Ui()

    x, y = findMultiColorInRegionFuzzy(0xd35600, "-30|0|0xf4b958,-5|-33|0xfdd170,20|0|0xf4b958,0|27|0xecb653,-78|51|0xccbdb0,-171|-52|0x6c2925,-339|-43|0x391412,-233|-110|0x396779,-435|220|0xedc530", 95, 295, 18, 1075, 429, 0, 0)
    if x > -1 then
        logger.debug("关卡.界面")
        return true;
    end

end

function GuanQia.UiClose()
    x, y = findMultiColorInRegionFuzzy(0xd35600, "-30|0|0xf4b958,-5|-33|0xfdd170,20|0|0xf4b958,0|27|0xecb653,-78|51|0xccbdb0,-171|-52|0x6c2925,-339|-43|0x391412,-233|-110|0x396779,-435|220|0xedc530", 95, 295, 18, 1075, 429, 0, 0)
    if x > -1 then
        shouzhi.click(x, y )
        logger.debug("关卡.界面关闭")
        return true;
    end

end

function GuanQia.find()


    x, y = findMultiColorInRegionFuzzy(0x6d0200, "-56|-35|0x010000,40|-32|0x010000,16|7|0x010000,-13|9|0x0e0000,-17|12|0x0a0000,16|13|0x010000", 95, 5, 450, 1067, 1527, 0, 0)
    if x > -1 then
        logger.debug("关卡.普通关卡")
        shouzhi.click(x, y - 20, 100 )
        mSleep(20)
        shouzhi.click(x, y - 20, 100)
        return true;
    end
end

function GuanQia.findBoss()
    x, y = findMultiColorInRegionFuzzy(0xfff2ce, "13|19|0xfff9dc,23|0|0xfff2ce,47|0|0xfff2ce,33|19|0xfff9dc,-43|18|0xefa535,-3|44|0xfde05d,37|41|0xf9c138,58|23|0xfccf41", 90, 11, 458, 1070, 870, 0, 0)
    if x > -1 then
        logger.debug("关卡.boss关卡")
        shouzhi.click(x, y, 100 )
        mSleep(20)
        shouzhi.click(x, y, 100)
        return true;
    end

end


function GuanQia.bossUi()
    x, y = findMultiColorInRegionFuzzy(0xf4fd31, "19|-8|0xf4fe31,-4|21|0xf1b81f,25|15|0xf2d827,80|28|0xf0b11d,106|14|0xf2ce25,111|31|0xef9818,116|-7|0xf4f92f,-121|-10|0xd39e37,-89|-27|0xd39e37", 90, 321, 837, 779, 953, 0, 0)
    if x > -1 then
        logger.debug("关卡.Boss界面")
        return true;
    end

end

function GuanQia.bossKaiZhan()
    x, y = findMultiColorInRegionFuzzy(0xf3e12a,"-28|9|0xf0af1d,-17|-17|0xf4fe31,55|15|0xf0b11d,87|18|0xef9617,81|0|0xf2d326,89|-17|0xf4fa2f,-148|-19|0xd39e37,-109|-40|0xd39e37", 95, 310, 819, 834, 1010, 0, 0)
    if x > -1 then
        shouzhi.click(x, y )
        logger.debug("关卡.开战Boss")
        return true;
    end

end
function GuanQia.bossZhuaBuChengGong()
    x, y = findMultiColorInRegionFuzzy(0xb5894e, "-15|2|0xa17841,-2|35|0xd0ae68,95|0|0xa07740,131|0|0xb5894e,115|40|0xd3b26b,220|0|0xb5894e,201|7|0xb99052,337|17|0xc19a59,329|53|0xdcbe74,340|61|0xe1c579,299|58|0x040405,197|67|0x030304,174|-44|0x060404,68|-13|0x020202,-42|12|0x010201", 90, 333, 326, 816, 515, 0, 0)
    if x > -1 then
        logger.debug("关卡.抓捕成功")
        shouzhi.click(x, y )
        return true;
    end
end

function GuanQia.BossJiangLi()
    x, y = findMultiColorInRegionFuzzy(0xbf8b48, "11|11|0xc89751,-17|46|0xefcb77,29|60|0xfbdb82,62|54|0xf5d47e,122|54|0xf5d47e,91|31|0xdeb466,70|7|0xc5934e,120|4|0xc28f4b,163|58|0xffe181,220|58|0xffde81,189|38|0xe6bf6e,201|18|0xcfa158,204|0|0xbf8b48,268|20|0xd2a45b,305|21|0xd3a55c,272|4|0xc28f4b,69|78|0x961914,197|85|0x8d1413,355|12|0xcfbe92", 90, 241, 597, 915, 883, 0, 0)
    if x > -1 then
        logger.debug("关卡.奖励Boss")
        shouzhi.click(x, y )
        return true;
    end
end

function GuanQia.BossXiaYiGuan()
    x, y = findMultiColorInRegionFuzzy(0x000000, "-104|108|0x5b2926,-134|114|0x612a28,362|108|0x622b2a,384|123|0x642927,396|-10|0x000000,531|53|0x000000,655|143|0x000000,501|1160|0x000000,104|47|0xf2c979,101|105|0xf1c46f,142|107|0xf1c46f,148|44|0xf0c678", 90, 241, 353, 878, 510, 0, 0)
    if x > -1 then
        logger.debug("关卡.下一关")
        shouzhi.click(x, y )
        return true;
    end
end

function GuanQia.juQing()
    logger.debug("关卡.剧情")
    shouzhi.click(981, 1883)
end


function GuanQia.KaiShiZhanDou()
    x, y = findMultiColorInRegionFuzzy(0x472420, "-105|-73|0x311d1d,81|-111|0x31201c,115|87|0x2f1d1d,10|43|0x86312f,-68|22|0x78312e,1|115|0x53181a,-83|-62|0x95343b,104|-62|0x733538,-52|-44|0xf6fc6d,75|-21|0xf3f94d,74|50|0xfafda2,-21|44|0xfcfec2,19|-36|0xf5fb5c", 95, 332, 881, 736, 1232, 0, 0)
    if x > -1 then
        shouzhi.click(x, y )
        logger.debug("关卡.开始战斗")
        return true;
    end
end
function GuanQia.ZhanBai()
    x, y = findMultiColorInRegionFuzzy(0x7f8184, "36|8|0xd8dbdc,55|-22|0xffffff,50|34|0xa7a7a9,40|48|0x7b7c80,130|-10|0xebf6fa,159|7|0x7c858d,171|-16|0x565d68,225|10|0xd3d7d5,195|50|0x7e0000,68|20|0x101d2b", 90, 347, 622, 783, 820, 0, 0)
    if x > -1 then
        shouzhi.click(x, y )
        logger.debug("关卡.开始战斗")
        return true;
    end
end

--
function GuanQia.zhanDouUi()
    x, y = findMultiColorInRegionFuzzy(0x602718, "-26|3|0xd7c591,-2|-26|0xd9c694,24|7|0xb38d58,1|25|0xb0884f,-11|-170|0x332712,11|-201|0x352a15,-9|-222|0x372d18,-37|-197|0x352914,-12|-195|0x2e1300", 95, 900, 86, 1076, 497, 0, 0)
    if x > -1 then
        logger.debug("关卡.战斗界面")
        return true;
    end
end

function GuanQia.binLiBuZu()
    x, y = findMultiColorInRegionFuzzy(0xffffff, "1|13|0xffffff,-130|-5|0xfcfcfc,221|5|0xffffff,241|5|0xffffff,126|138|0xfafda3,98|145|0xf7fd7f,31|133|0xfbfeba,19|105|0xfffffe,1|117|0xfffff8,63|131|0x86312f,68|96|0x4c2521,123|71|0xf4fa51", 90, 206, 896, 882, 1166, 0, 0)
    if x > -1 then

        logger.debug("关卡.兵力不足")
        return true;
    end
end

function GuanQia.ZhanDouUiClose()
    x, y = findMultiColorInRegionFuzzy(0x602718, "-26|3|0xd7c591,-2|-26|0xd9c694,24|7|0xb38d58,1|25|0xb0884f,-11|-170|0x332712,11|-201|0x352a15,-9|-222|0x372d18,-37|-197|0x352914,-12|-195|0x2e1300", 95, 900, 86, 1076, 497, 0, 0)
    if x > -1 then
        shouzhi.click(x, y )
        logger.debug("关卡.战斗界面关闭")
        return true;
    end
end






-- 关卡.战胜
function GuanQia.ZhanSheng()

    x, y = findMultiColorInRegionFuzzy(0x242026, "26|-38|0xecd4a2,176|-74|0x3d3541,157|6|0x29232b,101|15|0xac845f,211|8|0xb28c65,285|-34|0xe9d1a0,73|-107|0xdc5c3b,242|-116|0xdf5666,342|-77|0xdf5666", 95, 213, 432, 955, 822, 0, 0)
    if x > -1 then
        shouzhi.click(x, y )
        logger.debug("关卡.战胜")
        return true;
    end

end



function GuanQia.puTongGuanQia()
    gkFlag = true;
    local zdFlag = false; --战斗
    local runTotal = 0;
    -- 到关卡界面就退出
    while gkFlag do
        runTotal = runTotal + 1
        gkFlag = GuanQia.zhanDouUi()

        if gkFlag then
            zdFlag = gkFlag
        else
            GuanQia.find()
            GuanQia.juQing()
        end

        if runTotal > 10 then
            return;
        end
        gkFlag = not (gkFlag);
        mSleep(500)
    end

    local funcArr = { GuanQia.KaiShiZhanDou, GuanQia.ZhanSheng }
    while zdFlag do
        for index, func in ipairs(funcArr) do
            func()
        end
        zdFlag = GuanQia.Ui();
        zdFlag = not (zdFlag);
        mSleep(500)
    end

end


function GuanQia.BossGuanQia()
    gkFlag = true;
    local zdFlag = false; --战斗
    -- 到关卡界面就退出
    local runTotal = 0;
    while gkFlag do
        runTotal = runTotal + 1
        gkFlag = GuanQia.bossUi()

        if gkFlag then
            zdFlag = gkFlag
        else
            GuanQia.findBoss()
            GuanQia.juQing()
        end
        if runTotal > 10 then
            return;
        end

        gkFlag = not (gkFlag);
        mSleep(500)
    end

    local funcArr = { GuanQia.bossKaiZhan, GuanQia.BossJiangLi, GuanQia.BossXiaYiGuan, GuanQia.bossZhuaBuChengGong }
    while zdFlag do
        for index, func in ipairs(funcArr) do
            func()
        end
        zdFlag = GuanQia.Ui();
        zdFlag = not (zdFlag);
        mSleep(500)
    end

end


function GuanQia.run()
    local flag = false;
    logger.info("关卡.运行")
    while not (flag) do
        flag = GuanQia.Ui();
        if not (flag) then
            logger.debug(" 请切换至关卡.界面")
        end
        mSleep(500)
    end
    mSleep(1000)

    while flag do
        logger.debug("关卡.查找关卡")


        local gkFlag = GuanQia.findBoss();
        if gkFlag then
            GuanQia.BossGuanQia()
        end

        local gkFlag = GuanQia.find();
        if gkFlag then
            GuanQia.puTongGuanQia()
        end

        mSleep(100)

    end
end  
